# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     assertions.py
    Description:   
 -------------------------------------------------
 """
import re
from unittest import TestLoader

from playwright.sync_api import expect

from _hellchin_webrun.src.core.browser.base import BaseTest
from _hellchin_webrun.src.core.utils.chinese_alias import KeyWordManage
# from _hellchin_webrun.src.core.utils.logger.run_logger import TestLogger


class ElementAssertion(BaseTest):
    """元素属性断言"""

    def except_to_have_value(self, locator, expect_results, is_equal):
        """
        断言元素的value

        :param locator:
        :param expect_results:
        :param is_equal:
        :return:
        """

        if is_equal:
            expect(self.page.locator(locator)).to_have_value(re.compile(expect_results))
        else:
            expect(self.page.locator(locator)).not_to_have_value(re.compile(expect_results))

    def except_to_have_text(self, locator, expect_results, is_equal):
        """
        断言元素的文本

        :param locator:
        :param expect_results:
        :param is_equal:
        :return:
        """
        if is_equal:
            expect(self.page.locator(locator)).to_have_text(re.compile(expect_results))
        else:
            expect(self.page.locator(locator)).not_to_have_text(expect_results)

    def except_to_have_attribute(self, locator, name, value, is_equal):
        """
        断言元素的属性值
        :param locator:定位表达式
        :param name: 属性名称
        :param value: 属性值
        :param is_equal: 是否相等
        :return:
        """
        if is_equal:
            expect(self.page.locator(locator)).to_have_attribute(name, value)
        else:
            expect(self.page.locator(locator)).not_to_have_attribute(name, value)

    def except_to_be_visible(self, locator, index=1):
        """
        断言元素是否可见
        :param locator:
        :param index: 定位到的第几个元素
        :return:
        """
        if index > 1:
            expect(self.page.locator(locator).nth(index - 1)).to_be_visible()
        else:
            expect(self.page.locator(locator).first).to_be_visible()

    def except_to_be_hidden(self, locator, index=1):
        """
        断言元素不可见
        :param locator:
        :param index: 定位到的第几个元素
        :return:
        """
        if index > 1:
            expect(self.page.locator(locator).nth(index - 1)).to_be_hidden()
        else:
            expect(self.page.locator(locator).first).to_be_hidden()

    def except_to_be_enabled(self, locator, index=1):
        """
        断言元素是否可用
        :param locator:
        :param index: 定位到的第几个元素
        :return:
        """
        if index > 1:
            expect(self.page.locator(locator).nth(index - 1)).to_be_enabled()
        else:
            expect(self.page.locator(locator).first).to_be_enabled()

    def except_to_be_disabled(self, locator, index=1):
        """
        断言元素是否不可用
        :param locator:
        :param index: 定位到的第几个元素
        :return:
        """
        if index > 1:
            expect(self.page.locator(locator).nth(index - 1)).to_be_disabled()
        else:
            expect(self.page.locator(locator).first).to_be_disabled()

    def except_to_be_checked(self, locator, index=1):
        """
        断言元素是否被选中
        :param locator:
        :param index: 定位到的第几个元素
        :return:
        """
        if index > 1:
            expect(self.page.locator(locator).nth(index - 1)).to_be_checked()
        else:
            expect(self.page.locator(locator).first).to_be_checked()

    def except_to_be_empty(self, locator, index=1):
        """
        断言元素是否为空
        :param locator:
        :param index: 定位到的第几个元素
        :return:
        """
        if index > 1:
            expect(self.page.locator(locator).nth(index - 1)).to_be_empty()
        else:
            expect(self.page.locator(locator).first).to_be_empty()

    def except_to_be_editable(self, locator, index=1):
        """
        断言元素是否可编辑
        :param locator:
        :param index: 定位到的第几个元素
        :return:
        """
        if index > 1:
            expect(self.page.locator(locator).nth(index - 1)).to_be_editable()
        else:
            expect(self.page.locator(locator).first).to_be_editable()

    def except_to_be_focused(self, locator, index=1):
        """
        断言元素是否获取焦点
        :param locator:
        :param index: 定位到的第几个元素
        :return:
        """
        if index > 1:
            expect(self.page.locator(locator).nth(index - 1)).to_be_focused()
        else:
            expect(self.page.locator(locator).first).to_be_focused()


class PageAssertion(BaseTest):
    """页面级断言"""

    def assert_page_title(self, expect_results, is_equal=1):
        """
        断言页面的标题
        :param expect_results: 期望结果
        :param is_equal: 是否相等
        :return:
        """
        if is_equal:
            expect(self.page).to_have_title(re.compile(expect_results))
        else:
            expect(self.page).not_to_have_title(re.compile(expect_results))

    @KeyWordManage.chinese_alias("断言页面url地址")
    def assert_page_url(self, expect_results, is_equal=1):
        """
        断言页面的url地址
        :param expect_results: 期望结果
        :param is_equal: 是否相等
        :return:
        """
        if is_equal:
            TestLogger.start.suit_log(f"正在断言页面url地址:", f"预期结果：{expect_results},实际结果：{self.page.url}")

            expect(self.page).to_have_url(re.compile(expect_results))
            TestLogger.success.suit_log(f"正在断言页面url地址:", f"预期结果：{expect_results},实际结果：{self.page.url}")
        else:
            expect(self.page).not_to_have_url(re.compile(expect_results))


class AssertMixin(PageAssertion, ElementAssertion):
    """断言相关的方法封装"""
    pass
